//
//  MineCollectViewController.m
//  cityCar
//
//  Created by yao on 14-11-5.
//  Copyright (c) 2014年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "MineCollectViewController.h"
#import "DataBaseHandler.h"
#import "ForumGather.h"
#import "ForumDetailViewController.h"
#import "ForumContentViewController.h"
#import "MineCollectCell.h"

@interface MineCollectViewController ()<UITableViewDataSource, UITableViewDelegate>

@property(nonatomic, retain)UITableView *myTableView;

// 数据库数组
@property(nonatomic, retain)NSMutableArray *array;

@end

@implementation MineCollectViewController

- (void)dealloc
{
    [_myTableView release];
    [_array release];
    [super dealloc];
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self acquireDate];
    [self setupView];
    [self.myTableView reloadData];
    
}

- (void)viewDidAppear:(BOOL)animated
{
    DataBaseHandler *handler = [DataBaseHandler shareInstrance];
    if (self.temp == 10010) {
        self.array = [handler selectWebAll];
    } else if (self.temp == 10011){
        self.array = [handler selectModelAll];
    }

    if (self.array.count == 0) {
        [self.myTableView removeFromSuperview];
        self.navigationItem.rightBarButtonItem = nil;
        UIImageView *myImageView = [[UIImageView alloc] initWithFrame:CGRectMake((self.view.frame.size.width - 250) / 2, 100, 250, 200)];
        myImageView.image = [UIImage imageNamed:@"luntanjietu.jpg"];
        [self.view addSubview:myImageView];
        [myImageView release];
        
        UILabel *myLabel = [[UILabel alloc] initWithFrame:CGRectMake((self.view.frame.size.width - 200) / 2, 320, 200, 50)];
        myLabel.text = @"暂无收藏。。点击收藏按钮进行收藏。。";
        myLabel.numberOfLines = 0;
        myLabel.textAlignment = NSTextAlignmentCenter;
        [self.view addSubview:myLabel];
        [myLabel release];
        
    }
    
    [self.myTableView reloadData];
}

- (void)setupView
{
    // 屏幕宽和高
    CGFloat x = self.view.frame.size.width;
    CGFloat y = self.view.frame.size.height;
    
    self.view.backgroundColor = [WebColor white];
    
    if (self.array.count == 0) {
        UIImageView *myImageView = [[UIImageView alloc] initWithFrame:CGRectMake((x - 250) / 2, 100, 250, 200)];
        myImageView.image = [UIImage imageNamed:@"luntanjietu.jpg"];
        [self.view addSubview:myImageView];
        [myImageView release];
        
        UILabel *myLabel = [[UILabel alloc] initWithFrame:CGRectMake((x - 200) / 2, 320, 200, 50)];
        myLabel.text = @"暂无收藏。。点击收藏按钮进行收藏。。";
        myLabel.numberOfLines = 0;
        myLabel.textAlignment = NSTextAlignmentCenter;
        [self.view addSubview:myLabel];
        [myLabel release];
        
    } else {
        self.myTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, x, y - 49) style:UITableViewStylePlain];
        self.myTableView.backgroundColor = [WebColor whiteSmoke];
        self.myTableView.delegate = self;
        self.myTableView.dataSource = self;
        self.myTableView.rowHeight = 70;
        [self.myTableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
        [self.view addSubview:self.myTableView];
        [_myTableView release];
        
        // 编辑
        self.navigationItem.rightBarButtonItem = self.editButtonItem;
    }
    
}

#pragma mark - 编辑按钮
- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
    [super setEditing:editing animated:animated];
    [self.myTableView setEditing:editing animated:animated];
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        
        DataBaseHandler *handler = [DataBaseHandler shareInstrance];
        ForumGather *forum = [self.array objectAtIndex:indexPath.row];
        if (self.temp == 10010) {
            [handler deleteWeb:forum.Id];
        } else if (self.temp == 10011) {
            [handler deleteModel:forum.Id];
        }
        
        [self.array removeObjectAtIndex:indexPath.row];
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
        
        if (self.array.count == 0) {
            [self.myTableView removeFromSuperview];
            self.navigationItem.rightBarButtonItem = nil;
            UIImageView *myImageView = [[UIImageView alloc] initWithFrame:CGRectMake((self.view.frame.size.width - 250) / 2, 100, 250, 200)];
            myImageView.image = [UIImage imageNamed:@"luntanjietu.jpg"];
            [self.view addSubview:myImageView];
            [myImageView release];
            
            UILabel *myLabel = [[UILabel alloc] initWithFrame:CGRectMake((self.view.frame.size.width - 200) / 2, 320, 200, 50)];
            myLabel.text = @"暂无收藏。。点击收藏按钮进行收藏。。";
            myLabel.numberOfLines = 0;
            myLabel.textAlignment = NSTextAlignmentCenter;
            [self.view addSubview:myLabel];
            [myLabel release];
            
        }

    }
}

// 获取数据
- (void)acquireDate
{
    DataBaseHandler *handler = [DataBaseHandler shareInstrance];
    if (self.temp == 10010) {
        self.array = [handler selectWebAll];
    } else if (self.temp == 10011) {
        self.array = [handler selectModelAll];
    }
    
}

#pragma mark - tableview 协议
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.array.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.temp == 10010) {
        MineCollectCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuseMinec"];
        if (!cell) {
            cell = [[[MineCollectCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"reuseMinec"] autorelease];
        }
        ForumGather *forum = [self.array objectAtIndex:indexPath.row];
        // 判断是否有图片，没有用占位图片
        UIImage *image = [UIImage sd_imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:forum.image]]];
        if (image == nil) {
            cell.myImageView.image =[UIImage imageNamed:@"NoPicture.png"];
        } else {
            cell.myImageView.image = image;
        }
        
        cell.myLabel.text = forum.name;
        return cell;
    } else {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuseMineCollect"];
        if (!cell) {
            cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"reuseMineCollect"] autorelease];
        }
        
        ForumGather *forum = [self.array objectAtIndex:indexPath.row];
        cell.textLabel.text = forum.name;
        
        return cell;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.temp == 10010) {
        ForumContentViewController *contentVC = [[ForumContentViewController alloc] init];
        ForumGather *forum = [self.array objectAtIndex:indexPath.row];
        
        contentVC.forum.topicId = forum.Id;
        contentVC.forum.image = forum.image;
        contentVC.forum.title = forum.name;
        contentVC.forum.url = forum.url;
        [self presentViewController:contentVC animated:YES completion:^{
            
        }];
    } else if (self.temp == 10011) {
        ForumDetailViewController *detailVC = [[ForumDetailViewController alloc] init];
        ForumGather *forum = [self.array objectAtIndex:indexPath.row];
        detailVC.string = forum.Id;
        detailVC.dic = @{@"Name": forum.name};
        [self presentViewController:detailVC animated:YES completion:^{
            
        }];
    }
    
}

// tableView设置Cell的格式
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    //    ForumDetailCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    
    if ([cell respondsToSelector:@selector(tintColor)]) {
        
        CGFloat cornerRadius = 5.f;
        cell.backgroundColor = [UIColor clearColor];
        CAShapeLayer *layer = [[CAShapeLayer alloc] init];
        CGMutablePathRef pathRef = CGPathCreateMutable();
        CGRect bounds = CGRectInset(cell.bounds, 10, 0);
        bounds.size.width = self.view.bounds.size.width - 20;
        
        BOOL addLine = NO;
        if (indexPath.row == 0 && indexPath.row == [tableView numberOfRowsInSection:indexPath.section] - 1) {
            CGPathAddRoundedRect(pathRef, nil, bounds, cornerRadius, cornerRadius);
        } else if (indexPath.row  == 0) {
            
            CGPathMoveToPoint(pathRef, nil, CGRectGetMinX(bounds), CGRectGetMaxY(bounds));
            CGPathAddArcToPoint(pathRef, nil, CGRectGetMinX(bounds), CGRectGetMinY(bounds), CGRectGetMidX(bounds), CGRectGetMinY(bounds), cornerRadius);
            CGPathAddArcToPoint(pathRef, nil, CGRectGetMaxX(bounds), CGRectGetMinY(bounds), CGRectGetMaxX(bounds), CGRectGetMidY(bounds), cornerRadius);
            CGPathAddLineToPoint(pathRef, nil, CGRectGetMaxX(bounds), CGRectGetMaxY(bounds));
            addLine = YES;
        } else if (indexPath.row == [tableView numberOfRowsInSection:indexPath.section]-1) {
            
            CGPathMoveToPoint(pathRef, nil, CGRectGetMinX(bounds), CGRectGetMinY(bounds));
            CGPathAddArcToPoint(pathRef, nil, CGRectGetMinX(bounds), CGRectGetMaxY(bounds), CGRectGetMidX(bounds), CGRectGetMaxY(bounds), cornerRadius);
            CGPathAddArcToPoint(pathRef, nil, CGRectGetMaxX(bounds), CGRectGetMaxY(bounds), CGRectGetMaxX(bounds), CGRectGetMidY(bounds), cornerRadius);
            CGPathAddLineToPoint(pathRef, nil, CGRectGetMaxX(bounds), CGRectGetMinY(bounds));
        } else {
            
            CGPathAddRect(pathRef, nil, bounds);
            addLine = YES;
        }
        layer.path = pathRef;
        CFRelease(pathRef);
        layer.fillColor = [UIColor colorWithWhite:1.f alpha:0.8f].CGColor;
        layer.strokeColor = [UIColor lightGrayColor].CGColor;
        
        if (addLine == YES) {
            
            CALayer *lineLayer = [[CALayer alloc] init];
            CGFloat lineHeight = (1.f / [UIScreen mainScreen].scale);
            lineLayer.frame = CGRectMake(CGRectGetMinX(bounds)+10, bounds.size.height-lineHeight, bounds.size.width-10, lineHeight);
            lineLayer.backgroundColor = tableView.separatorColor.CGColor;
            [layer addSublayer:lineLayer];
        }
        
        UIView *testView = [[UIView alloc] initWithFrame:bounds];
        [testView.layer insertSublayer:layer atIndex:0];
        testView.backgroundColor = UIColor.clearColor;
        cell.backgroundView = testView;
    }
    
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
 {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
